package com.itlong.cloud.operate.service.impl;


import com.itlong.cloud.POJO.DTO.operate.OperateDeviceWhiteListAuthFileConsumeRecordsDTO;
import com.itlong.cloud.POJO.PO.DeviceWhiteListAuthFileConsumeRecordsPO;
import com.itlong.cloud.POJO.VO.operate.OperateDeviceWhiteListAuthFileConsumeRecordsVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateDeviceWhiteListAuthFileConsumeRecordsDao;
import com.itlong.cloud.operate.service.IOperateDeviceWhiteListAuthFileConsumeRecordsService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.date.DateUtil;
import com.itlong.cloud.utils.page.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * <desc>
 * 运营-设备白名单授权文件消费记录管理服务接口实现类
 * </desc>
 * 
 * @author: chengshican
 *  @Date: 2020-08-11 14:34
 *  
 */
@Service
public class OperateDeviceWhiteListAuthFileConsumeRecordsServiceImpl implements IOperateDeviceWhiteListAuthFileConsumeRecordsService {
     @Autowired
     private IOperateDeviceWhiteListAuthFileConsumeRecordsDao recordsDao;

    /**
     * <desc>
     * 保存授权文件消费信记录信息
     * </desc>
     *
     * @param recordsDTO
     * @Author chengshican
     * @Date 2020-08-11 11:23
     * @Param recordsDTO 保存参数DTO
     * @Return 保存结果
     * @Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer save(OperateDeviceWhiteListAuthFileConsumeRecordsDTO recordsDTO) throws Exception {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        Date nowTime= new Date();
        DeviceWhiteListAuthFileConsumeRecordsPO recordsPO = new DeviceWhiteListAuthFileConsumeRecordsPO();
        BeanUtils.copyProperties(recordsDTO,recordsPO);
        recordsPO.setCreateTime(nowTime);
        recordsPO.setUpdateTime(nowTime);
        recordsPO.setCreateUserId("device操作");
        recordsPO.setOperateUserId("deviceUpdate");
        recordsPO.setStatus(1);
        recordsPO.setDeviceActivateTime(df.parse(recordsDTO.getDeviceActivateTime()));
        Integer result = recordsDao.save(SqlUtil.durableData(recordsPO, PlatformConstants.TABLE_SAVE));
        return result;
    }

    /**
     * <desc>
     * 分页获取设备白名单消费授权文件消费记录信息
     * </desc>
     *
     * @param recordsDTO
     * @Author chengshican
     * @Date 2020-08-11 14:20
     * @Param recordsDTO 查询参数DTO
     * @Return
     * @Exception
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateDeviceWhiteListAuthFileConsumeRecordsVO> getWhiteListAuthFileConsumeRecordsByPage(OperateDeviceWhiteListAuthFileConsumeRecordsDTO recordsDTO) throws Exception {

        Map<String, Object> params = SqlUtil.convertBeanToMapParams(recordsDTO);
        Page<OperateDeviceWhiteListAuthFileConsumeRecordsVO> page = new Page<>();
        page.getPage().setPageSize(recordsDTO.getPageSize());
        page.getPage().setCurrentPage(recordsDTO.getCurrentPage());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        page.setRows(recordsDao.getWhiteListAuthFileConsumeRecordsByPage(params),recordsDao.getWhiteListAuthFileConsumeRecordsPageCount(params));
        return page;
    }

    /**
     * <desc>
     * 根据授权规则id  ruleId查询授权文件消费信息
     * </desc>
     *
     * @param ruleId
     * @Author chengshican
     * @Date 2020-08-11 14:22
     * @Param ruleId 规则id
     * @Return 授权文件消费信息
     * @Exception
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public OperateDeviceWhiteListAuthFileConsumeRecordsVO getWhiteListAuthFileConsumeRecordsByRuleId(String ruleId) {
         return recordsDao.getWhiteListAuthFileConsumeRecordsByRuleId(ruleId);
    }
}
